﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.IO;
using System.Net;
using System.Xml;

namespace BaseData.UI
{
    /// <summary>
    /// 远程接口调用
    /// </summary>
    public class SearchInterface
    {
        /// <summary>
        /// 接口超时时间
        /// </summary>
        private static string timeout = System.Configuration.ConfigurationManager.AppSettings["InterfaceTimeout"];

        /// <summary>
        /// 获取远程接口函数
        /// </summary>
        /// <param name="param">参数，可以为空</param>
        /// <param name="url">远程接口地址</param>
        /// <param name="method">值为POST或GET</param>
        /// <param name="reLength">获取数据最大长度</param>
        /// <param name="ds">返回数据对象</param>
        /// <returns>执行结果返回</returns>
        public static bool GetSearchData(string param, string url, string method, int reLength, ref DataSet ds)
        {
            bool isSucc = true;
            StringBuilder sb = GetSearchData(param, url, method, reLength);
            if (sb != null && sb.Length > 0)
            {
                try
                {
                    System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
                    xd.LoadXml(sb.ToString());
                    System.Xml.XmlNodeReader xnr = new System.Xml.XmlNodeReader(xd);
                    ds.ReadXml(xnr);
                    xnr.Close();
                }
                catch
                {
                    isSucc = false;
                }
            }
            return isSucc;
        }

        /// <summary>
        /// 获取远程接口函数
        /// </summary>
        /// <param name="Params">参数，可以为空</param>
        /// <param name="Url">远程接口地址</param>
        /// <param name="ds">返回数据对象</param>
        /// <returns>执行结果返回</returns>
        public static bool GetSearchData(string Params, string Url, ref DataSet ds)
        {
            return GetSearchData(Params, Url, "POST", 9999, ref  ds);
        }

        /// <summary>
        /// 获取远程接口函数
        /// </summary>
        /// <param name="Params">参数，可以为空</param>
        /// <param name="Url">远程接口地址</param>
        /// <returns>返回结果</returns>
        public static StringBuilder GetSearchData(string Params, string Url)
        {
            return GetSearchData(Params, Url, "POST", 9999);
        }

        /// <summary>
        /// 获取远程接口函数
        /// </summary>
        /// <param name="param">参数，可以为空</param>
        /// <param name="url">远程接口地址</param>
        /// <param name="method">值为POST或GET</param>
        /// <param name="reLength">获取数据最大长度</param>
        /// <returns>执行结果返回</returns>
        public static StringBuilder GetSearchData(string Params, string Url, string Method, int ReLength)
        {
            //把sXmlMessage发送到指定的DsmpUrl地址上
            //动态设置超时时间
            string tOut = timeout;
            int timeOut = String.IsNullOrEmpty(tOut) ? 8 : Int32.Parse(tOut);

            Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
            byte[] arrB = encode.GetBytes(Params);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(Url);
            myReq.Method = Method;
            myReq.Timeout = 1000 * timeOut;
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();

            //接收HTTP做出的响应
            WebResponse myResp = myReq.GetResponse();
            Stream ReceiveStream = myResp.GetResponseStream();
            string str = string.Empty;
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[ReLength];
            int count = readStream.Read(read, 0, ReLength);

            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, ReLength);
            }
            readStream.Close();
            myResp.Close();
            return new StringBuilder(str);
        }
    }
}